package cockpit.desarrollo.DAO;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import cockpit.DAO.interfaz.IBanco_TipoCuenta;
import cockpit.comm.DTO.DatosException;
import cockpit.desarrollo.ConectDB.Constantes;



public class Banco_TipoCuentaDAO implements IBanco_TipoCuenta
{

    private Connection connect = null;

    private ResultSet resultSet = null;

    public Banco_TipoCuentaDAO( )
    {

    }

    @Override
    public Connection getConnection( ) throws DatosException
    {
        try
        {
            String driverName = Constantes.DRIVERNAME; // NetDirect JDBC driver
            String serverName = Constantes.SERVERNAME;
            String portNumber = Constantes.PORTNUMBER;
            String squema = Constantes.SQUEMA;
            String mydatabase = serverName + ":" + portNumber + "/" + squema;
            String url = Constantes.URL + mydatabase; // a JDBC url
            
            // Load the JDBC driver
            Class.forName( driverName );

            // Create a connection to the database
            connect = DriverManager.getConnection( url, Constantes.USER, Constantes.PASSWORD );
            return connect;
        }
        catch( ClassNotFoundException e )
        {
            System.out.println("error: "+e.getMessage( ));
            throw new DatosException( "Could not find the database driver" );
        }
        catch( SQLException e )
        {
            throw new DatosException( "Could not connect to the database" );
        }
    }

    @Override
    public void closeConnection( ) throws DatosException
    {
        try
        {
            connect.close( );
        }
        catch( SQLException e )
        {
            throw new DatosException( e.getMessage( ) );
        }
        connect = null;
    }

    private void inicializar( ) throws DatosException
    {
        try
        {
            connect = getConnection( );
        }
        catch( DatosException e )
        {
            throw new DatosException( e.getMessage( ) );
        }
    }

    @Override
    public long insertTipoCuenta( int prmTipoCuenta, String prmDescripcion) throws DatosException
    {

        try
        {

            inicializar( );

            connect.setAutoCommit( false );

            String consulta =   "INSERT INTO TipoCuenta" + 
                                "(" +
                                " " + 
                                "tipoCuenta," + 
                                "descripcion" + 
                                " "+
                                ")" +
                                "VALUES(?,?) ";

            String consulta2 = "SELECT last_insert_id() as prmInsertId; ";

            PreparedStatement stmt = connect.prepareStatement( consulta );

            PreparedStatement stmt2 = connect.prepareStatement( consulta2 );

            stmt.setInt( 1, prmTipoCuenta );           
            stmt.setString( 2, prmDescripcion );

            long prmLongIdTransaccion = 0;
            if( stmt != null )
            {
                stmt.execute( );
                resultSet = stmt2.executeQuery( );

                if( resultSet.next( ) )
                {
                    prmLongIdTransaccion = resultSet.getLong( "prmInsertId" );
                }

            }

            connect.commit( );

            return prmLongIdTransaccion;
        }
        catch( Exception e )
        {
            try
            {
                connect.rollback( );
            }
            catch( SQLException e1 )
            {
                e1.printStackTrace( );
            }
            e.printStackTrace( );
            throw new DatosException( e.getMessage( ) );
        }
        finally
        {
            closeConnection( );
        }

    }
    
    @Override
    public long insertBanco( String prmNombreBanco, String prmDescripcion) throws DatosException
    {
        try
        {
            inicializar( );

            connect.setAutoCommit( false );

            String consulta =   "INSERT INTO Banco" + 
                                "(" +
                                " " + 
                                "nombreBanco," + 
                                "descripcion" + 
                                " "+
                                ")" +
                                "VALUES(?,?) ";

            String consulta2 = "SELECT last_insert_id() as prmInsertId; ";

            PreparedStatement stmt = connect.prepareStatement( consulta );

            PreparedStatement stmt2 = connect.prepareStatement( consulta2 );

            stmt.setString( 1, prmNombreBanco );           
            stmt.setString( 2, prmDescripcion );

            long prmLongIdTransaccion = 0;
            if( stmt != null )
            {
                stmt.execute( );
                resultSet = stmt2.executeQuery( );

                if( resultSet.next( ) )
                {
                    prmLongIdTransaccion = resultSet.getLong( "prmInsertId" );
                }

            }

            connect.commit( );

            return prmLongIdTransaccion;
        }
        catch( Exception e )
        {
            try
            {
                connect.rollback( );
            }
            catch( SQLException e1 )
            {
                e1.printStackTrace( );
            }
            e.printStackTrace( );
            throw new DatosException( e.getMessage( ) );
        }
        finally
        {
            closeConnection( );
        }
    }

}
